package org.example.service.impl;

import org.example.dao.MaterialDao;
import org.example.dao.impl.MaterialDaoImpl;
import org.example.entity.Material;
import org.example.other.ErrorEnum;
import org.example.other.ServiceException;
import org.example.service.MaterialService;
import org.example.util.DruidUtil;

import java.sql.Connection;

public class MaterialServiceImpl implements MaterialService {
    MaterialDao materialDao;
    public MaterialServiceImpl() {
        this.materialDao = new MaterialDaoImpl();
    }
    @Override
    public void save(Material material) throws Exception {
        Connection conn = null;
        try{
            conn = DruidUtil.getConnection();
            if (materialDao.exists(conn, material)) {
                throw new ServiceException(ErrorEnum.MATERIAL_EXISTS);
            }else {
                materialDao.insert(conn, material);
            }
        } finally {
            DruidUtil.closeConnection(conn);
        }
    }
}
